#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
@author: yinzhuoqun
@site: http://zhuoqun.info/
@email: yin@zhuoqun.info
@time: 2024/4/18 11:44
"""

# pip install flask
from flask import Flask, jsonify, request
from flask_cors import CORS
from method import *

app = Flask(__name__)
CORS(app)  # 允许所有跨域请求


@app.route('/')
def hello_world():
    return jsonify({'message': 'Hello, World!'})


@app.route('/trace_board_data/', methods=['GET', 'POST'])
def api_save_trace_board_data():
    """
    保存 trace_board 数据
    :return:
    """
    msg, code = {}, 200
    if request.method == 'POST':
        if request.content_type.startswith('application/json'):
            nickname = request.json.get('nickname', None)
            count = request.json.get('todayCount', None)
            mac_serial = request.json.get('macSerial', None)
        elif request.content_type.startswith('multipart/form-data'):
            nickname = request.form.get('nickname', None)
            count = request.form.get('todayCount', None)
            mac_serial = request.form.get('macSerial', None)
        else:
            nickname = request.values.get("nickname", None)
            count = request.values.get("todayCount", None)
            mac_serial = request.values.get("macSerial", None)
        msg, code = save_trace_board_data(nickname, mac_serial, count)

    return msg, code


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)
